import * as gulp from "gulp";
import * as ts from "gulp-typescript";
import * as sourcemaps from "gulp-sourcemaps";
import * as del from "del";
import * as jsonfile from "jsonfile";
import * as gutil from "gulp-util";

const tslint = require("gulp-tslint");
const prettier = require("gulp-prettier-plugin");

const src = ["./src/**/*.{ts,tsx}"];
const tsProj = ts.createProject("tsconfig.json");

gulp.task("typescript", () => {
  return gulp
    .src(src)
    .pipe(sourcemaps.init())
    .pipe(tsProj())
    .pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "../src" }))
    .pipe(gulp.dest("dist"));
});

gulp.task("prettier", () => {
  const cfg = jsonfile.readFileSync("./.prettierrc", { throws: false });
  if (cfg === null) {
    throw new gutil.PluginError({
      plugin: "prettier",
      message: "missing or deformed .prettierrc"
    });
  }
  return gulp
    .src(src)
    .pipe(prettier(cfg, { filter: true }) as any)
    .pipe(gulp.dest((file: any) => file.base));
});

gulp.task("tslint", () =>
  gulp
    .src(src)
    .pipe(
      tslint({
        formatter: "stylish"
      })
    )
    .pipe(
      tslint.report({
        allowWarnings: true,
        summarizeFailureOutput: true
      })
    )
);

gulp.task("copyFiles", () => {
  return gulp.src(["src/**/*.{ini,json}"]).pipe(gulp.dest("dist"));
});

gulp.task("clean", () => {
  return del(["dist/*", "!.gitkeep"]);
});

gulp.task("build", gulp.series("clean", "typescript", "copyFiles"));

gulp.task("watch:build", () => {
  gulp.watch(src, gulp.series("build"));
});
